#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os
import sys
import string
import MySQLdb
import time
from Tools import connectMySQL

pid_table = "s_partner_new"
stat_main = "stat.stat_day_main"
stat_paike = "stat.stat_day_paike"
stat_factory = "stat.stat_day_factory"
stat_channel = "stat.stat_day_channel"

def getPidStat():
    """
    获取对应PID的OS_ID、F_ID、BUSINESS_TYPE、DEVICE_TYPE、CLIENT_TYPE、PARTNER_MODE
    """
    global pid_os
    global pid_f
    global pid_business
    global pid_device
    global pid_client
    global pid_partnermode
    global pid_stat

    try:
        db = connectMySQL.connMySQL_mobileProduction()
    except MySQLdb.Error, e:
        print e
        resultStr = '{"status": "failed", "code": "4001", "str": "connect database failed"}'
        return resultStr
    cursor = db.cursor()

    try:
        sql = "SELECT pid,os_id,f_id,business_type,device_type,client_type,partner_mode,pid_stat FROM %s" % pid_table
        cursor.execute(sql) 
        resultSet=cursor.fetchall()
    except MySQLdb.Error, e:
        print e
        resultStr='{"status": "failed", "typeID": "%s", "code": "4002", "str": "execute SQL failed"}' % typeID
        return resultStr

    pid_os = {}
    pid_f = {}
    pid_business = {}
    pid_device = {}
    pid_client = {}
    pid_partnermode = {}
    pid_stat = {}

    for data in resultSet:
        pid_os[data[0]] = data[1]
        pid_f[data[0]] = data[2]
        pid_business[data[0]] = data[3]
        pid_device[data[0]] = data[4]
        pid_client[data[0]] = data[5]
        pid_partnermode[data[0]] = data[6]
        pid_stat[data[0]] = data[7]

    db.close()

def upPidStat(stat_type,argv=None):
    argv = sys.argv
    logdate = argv[1]
    """
    根据对应PID的OS_ID、F_ID、BUSINESS_TYPE、DEVICE_TYPE、CLIENT_TYPE、PARTNER_MODE，更新对应的结果表字段
    """

    try:
        db = connectMySQL.connMySQL_mobileProduction()
    except MySQLdb.Error, e:
        print e
        resultStr = '{"status": "failed", "code": "4001", "str": "connect database failed"}'
        return resultStr
    cursor = db.cursor()

    try:
        for key in pid_os:
            if(stat_type == "main"):
                sql = "UPDATE %s SET business_type = %s, device_type = %s, f_id = %s, os_id = %s, client_type = %s, partner_mode = %s, pid_stat = %s WHERE pid = '%s' and date = %s" % (stat_main, pid_business[key], pid_device[key], pid_f[key], pid_os[key], pid_client[key], pid_partnermode[key], pid_stat[key], key, logdate)
            elif(stat_type == "paike"):
                sql = "UPDATE %s SET business_type = %s, device_type = %s, f_id = %s, os_id = %s, client_type = %s, partner_mode = %s, pid_stat = %s WHERE pid = '%s' and date = %s" % (stat_paike, pid_business[key], pid_device[key], pid_f[key], pid_os[key], pid_client[key], pid_partnermode[key], pid_stat[key], key, logdate)
            elif(stat_type == "factory"):
                sql = "UPDATE %s SET business_type = %s, device_type = %s, f_id = %s, os_id = %s, client_type = %s, partner_mode = %s, pid_stat = %s WHERE pid = '%s' and date = %s" % (stat_factory, pid_business[key], pid_device[key], pid_f[key], pid_os[key], pid_client[key], pid_partnermode[key], pid_stat[key], key, logdate)
            elif(stat_type == "channel"):
                sql = "UPDATE %s SET business_type = %s, device_type = %s, f_id = %s, os_id = %s, client_type = %s, partner_mode = %s, pid_stat = %s WHERE pid = '%s' and date = %s" % (stat_channel, pid_business[key], pid_device[key], pid_f[key], pid_os[key], pid_client[key], pid_partnermode[key], pid_stat[key], key, logdate)
            cursor.execute(sql) 
    except MySQLdb.Error, e:
        print e
        resultStr='{"status": "failed", "code": "4002", "str": "execute SQL failed"}'
        return resultStr

    db.close()

if __name__ == '__main__':
    starttime = time.time()
    getPidStat()
    upPidStat("main")
    upPidStat("paike")
    upPidStat("factory")
    upPidStat("channel")
    endtime = time.time()
    print "Run times:" + str(int((endtime - starttime)))  
